package com.kj.xuyuan.home;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.MainPageAdapter;
import com.kj.xuyuan.base.BaseFragment;
import com.kj.xuyuan.model.EventBusBean;
import com.kj.xuyuan.util.LogUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.OnClick;

public class HomeFragment extends BaseFragment {

    @BindView(R.id.iv_sign)
    ImageView ivSign;
    @BindView(R.id.iv_msg)
    ImageView ivMsg;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.tv_recommend)
    TextView tvRecommend;
    @BindView(R.id.tv_near)
    TextView tvNear;
    @BindView(R.id.tv_dynamic)
    TextView tvDynamic;
    @BindView(R.id.tv_friend)
    TextView tvFriend;
    @BindView(R.id.tv_recommend_line)
    TextView tvRecommendLine;
    @BindView(R.id.tv_near_line)
    TextView tvNearLine;
    @BindView(R.id.tv_dynamic_line)
    TextView tvDynamicLine;
    @BindView(R.id.tv_friend_line)
    TextView tvFriendLine;

    private List<Fragment> fragments;
    private MainPageAdapter pageAdapter;
    private int currentPage;

    @Override
    protected int setLayoutResourceID() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        fragments = new ArrayList<>();
        fragments.add(RecommendFragment.newInstance(1));
        fragments.add(RecommendFragment.newInstance(2));
        fragments.add(DynamicFragment.newInstance(1,""));
        fragments.add(DynamicFragment.newInstance(2,""));
        pageAdapter = new MainPageAdapter(getChildFragmentManager(),fragments);
        viewpager.setOffscreenPageLimit(0);
        viewpager.setAdapter(pageAdapter);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                LogUtils.e(TAG,"position ="+position);
                currentPage = position;
                changeView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick({R.id.iv_sign, R.id.iv_msg,R.id.tv_recommend,R.id.tv_near,R.id.tv_dynamic,R.id.tv_friend})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_sign:
                EventBusBean busBean = new EventBusBean();
                busBean.setCode(1001);
                EventBus.getDefault().post(busBean);
                break;
            case R.id.iv_msg:
                MsgListActivity.actionStar(getContext());
                break;
            case R.id.tv_recommend:
                if(currentPage != 0){
                    currentPage = 0;
                    changeView();
                }
                break;
            case R.id.tv_near:
                if(currentPage != 1){
                    currentPage = 1;
                    changeView();
                }
                break;
            case R.id.tv_dynamic:
                if(currentPage != 2){
                    currentPage = 2;
                    changeView();
                }
                break;
            case R.id.tv_friend:
                if(currentPage != 3){
                    currentPage = 3;
                    changeView();
                }
                break;
        }
    }

    private void changeView() {
        viewpager.setCurrentItem(currentPage);
        tvRecommend.setTextColor(getResources().getColor(R.color.gray9));
        tvRecommendLine.setVisibility(View.INVISIBLE);
        tvNear.setTextColor(getResources().getColor(R.color.gray9));
        tvNearLine.setVisibility(View.INVISIBLE);
        tvDynamic.setTextColor(getResources().getColor(R.color.gray9));
        tvDynamicLine.setVisibility(View.INVISIBLE);
        tvFriend.setTextColor(getResources().getColor(R.color.gray9));
        tvFriendLine.setVisibility(View.INVISIBLE);
        switch (currentPage){
            case 0:
                tvRecommend.setTextColor(getResources().getColor(R.color.color_333));
                tvRecommendLine.setVisibility(View.VISIBLE);
                break;
            case 1:
                tvNear.setTextColor(getResources().getColor(R.color.color_333));
                tvNearLine.setVisibility(View.VISIBLE);
                break;
            case 2:
                tvDynamic.setTextColor(getResources().getColor(R.color.color_333));
                tvDynamicLine.setVisibility(View.VISIBLE);
                break;
            case 3:
                tvFriend.setTextColor(getResources().getColor(R.color.color_333));
                tvFriendLine.setVisibility(View.VISIBLE);
                break;
        }
    }
}
